package com.nbgroup.service.impl;

import com.nbgroup.mapper.LoginMapper;
import com.nbgroup.pojo.User;
import com.nbgroup.pojo.common.LoginInfo;
import com.nbgroup.pojo.vo.GetInfoVO;
import com.nbgroup.pojo.vo.LoginVO;
import com.nbgroup.service.LoginService;
import com.nbgroup.until.JwtProperties;
import com.nbgroup.until.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public LoginVO login(LoginInfo loginInfo) {
        log.info("loginInfo:{}",loginInfo);
        User user = loginMapper.login(loginInfo);
        log.info("user:{}",user);
        if (user!=null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("user_id", user.getUserId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);
            return new LoginVO("登录成功",200,token);
        }else {
            return new LoginVO("登录失败",500,"");
        }
    }


    @Override
    public GetInfoVO getInfo() {
        GetInfoVO getInfoVO = new GetInfoVO();
        List roles = new ArrayList<>();
        roles.add("admin");
        List permissions = new ArrayList<>();
        permissions.add("*:*:*");
        getInfoVO.setPermissions(permissions);
        getInfoVO.setRoles(roles);
        User user = new User();
        user.setUserName("admin");
        getInfoVO.setMsg("操作成功");
        getInfoVO.setCode(200);
        getInfoVO.setUser(user);
        return getInfoVO;
    }
}
